package cn.sunline.test.user;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sunline.test.user.model.User;

@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient
@RequestMapping("/user")
public class UserApplication {
	private static final Logger log = LoggerFactory.getLogger(UserApplication.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public static void main(String[] args) {
		SpringApplication.run(UserApplication.class, args);
	}
	
	@GetMapping("/hello/{name}")
	@ResponseBody
	public String sayHello(@RequestParam(value = "name") String name) {
		return "hai : " + name;
	}
	
	@PostMapping
	@ResponseBody
	public User add(@RequestBody User user) {
		log.info("user info: " + user.toString());
		jdbcTemplate.update("INSERT INTO user(user_name, email, create_time) " 
				+ "values(?, ?, ?)",user.getUser_name(),user.getEmail(),new Date());
		user = jdbcTemplate.queryForObject("select * from user where user_name=?", new Object[]{user.getUser_name()},
				new BeanPropertyRowMapper<>(User.class));
		return user;
	}

}
